package cn.nexd.app.collector.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.adapter.ErrorListAdapter;
import cn.nexd.app.collector.model.FileManagerModel;
import cn.nexd.app.collector.presenter.fileManager.ErrorFileManagerPresenter;
import cn.nexd.app.collector.presenter.fileManager.FileManagerContract;
import cn.nexd.app.collector.util.ScreenUtils;
import cn.nexd.app.collector.util.UMengUtil;
import cn.nexd.app.collector.widget.CustomRecyclerView;

/**
 * Created by xunzongxia on 16/10/9.
 */

public class ErrorFileFragment extends Fragment implements FileManagerContract.View {
    private TextView emptyView;
    private CustomRecyclerView rlLogs;

    private BottomSheetDialog fileInfoDialog;
    private View dialogView;
    private static final String TAG = "ErrorFileFragment";
    private TextView tvFileName;
    private ExpandableListView errorList;
    private List<FileManagerModel> fileManagerModels;
    private ErrorListAdapter errorListAdapter;
    private AlertDialog errorLogDialog;
    private TextView tvLog;

    private FileManagerContract.Presenter presenter;

    public static ErrorFileFragment newInstance(int position) {

        Bundle args = new Bundle();

        ErrorFileFragment fragment = new ErrorFileFragment();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new ErrorFileManagerPresenter(this.getActivity(), this);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        UMengUtil.getInstance().onResume(this.getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        UMengUtil.getInstance().onPause(this.getActivity());
    }

    //测试用的假数据
    private void initData() {
        createErrorLog();
    }

    private void createErrorLog() {
        fileManagerModels = new ArrayList<>();
        presenter.loadFile();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_error_info, container, false);
        initView(layout);
        bindAdapter();
        BindItemClick();
        return layout;
    }

    private void BindItemClick() {
        errorList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                fileManagerModels.get(groupPosition).setChildFileNames(presenter.loadChildFilesName(fileManagerModels.get(groupPosition).getParentName()));
                return false;
            }
        });
        errorList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                if (errorLogDialog.isShowing()) {
                    errorLogDialog.dismiss();
                } else {
                    tvLog.scrollTo(0, 0);
                    tvLog.setText(presenter.loadFileContent(fileManagerModels.get(groupPosition).getChildFileNames().get(childPosition)));
                    errorLogDialog.show();
                    Window window = errorLogDialog.getWindow();
                    WindowManager.LayoutParams layoutParams = window.getAttributes();
                    layoutParams.width = (int) (ScreenUtils.getScreenWidth(getContext()) * 0.8);
                    layoutParams.height = (int) (ScreenUtils.getScreenWidth(getContext()) * 0.9);
                    window.setAttributes(layoutParams);
                }
                return false;
            }
        });
        errorList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < errorListAdapter.getGroupCount(); i++) {
                    if (groupPosition != i) {
                        errorList.collapseGroup(i);
                    }
                }
            }
        });
    }


    private void initView(View layout) {
        errorList = (ExpandableListView) layout.findViewById(R.id.error_log_list);
        createDialog();

    }

    private void createDialog() {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_error_log, null);
        tvLog = (TextView) dialogView.findViewById(R.id.dialog_tv_error);
        tvLog.setMovementMethod(ScrollingMovementMethod.getInstance());
        errorLogDialog = new AlertDialog.Builder(getContext()).setView(dialogView).setCancelable(true).create();
    }


    private void bindAdapter() {
        errorList.setGroupIndicator(null);
        errorListAdapter = new ErrorListAdapter(getContext(), fileManagerModels);
        errorList.setAdapter(errorListAdapter);
    }

    @Override
    public void updateList(List<FileManagerModel> logs) {
        fileManagerModels.addAll(logs);
        errorListAdapter.notifyDataSetChanged();
    }

    @Override
    public void initView() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void startActivity() {

    }

    @Override
    public void printLog(String msg) {

    }
}
